package com.atguigu.weixin.user.group;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.atguigu.crud.utils.JsonUtil;
import com.atguigu.weixin.kit.WeixinKit;
import com.atguigu.weixin.model.WeixinFianlValue;
import com.atguigu.weixin.user.model.WGroup;

@Service
public class WGroupService implements IWGroupService {

	@Override
	public void add(WGroup group) {
		      String url=WeixinKit.replaceAccToken(WeixinFianlValue.ADD_GROUP);
              Map<String,WGroup> map=new HashMap<String,WGroup>();
              map.put("group", group);
              String json=JSON.toJSONString(map);
            String con = WeixinKit.postReq(url, json, "application/json");
            System.out.println(con);
	}

	//查询所有用户分组
	@Override
	public List<WGroup> queryAll() {
		String url=WeixinKit.replaceAccToken(WeixinFianlValue.QUERY_ALL_GROUP);
		String json=WeixinKit.getReq(url);
		System.out.println(json);
		//json数据转成对象集合
		System.out.println("#########################################");
		
		//json转map对象
		Map<String, Object> maps= JsonUtil.jsonToMap(json);
		System.out.println(maps);
		
		List<Map> listMap=(List<Map>) maps.get("groups");
		List<WGroup> wgs=new ArrayList<WGroup>();
		
		for(Map m:listMap){
			WGroup w=new WGroup();
			w.setId((Integer)m.get("id"));
			w.setName((String)m.get("name"));
			w.setCount((Integer)m.get("count"));
			wgs.add(w);
		}
		
		return wgs;
	}

	//用户所在的分组
	@Override
	public WGroup queryUserGroup(String openid) {
		List<WGroup> wgs=queryAll();
		
		String url=WeixinKit.replaceAccToken(WeixinFianlValue.QUERY_USER_GROUP);
		Map<String,String> map=new HashMap<String,String>();
		map.put("openid", openid);
		String con =WeixinKit.postReq(url, JSON.toJSONString(map), "application/json");
		System.out.println(con);
		
		Map<String,Object> gm=JsonUtil.jsonToMap(con);
		
		Integer gid=(Integer) gm.get("groupid");
		
		for (WGroup w : wgs) {
			 if(w.getId()==gid){
				 return w;
			 }
		}
		return null;
	}

	@Override
	public void updateGroupName(int gid, String name) {
		String url = WeixinKit.replaceAccToken(WeixinFianlValue.UPDATE_GROUP_NAME);
		Map<String,WGroup> wmap = new HashMap<String,WGroup>();
		WGroup wg = new WGroup();
		wg.setId(gid);
		wg.setName(name);
		wmap.put("group", wg);
		String json = JSON.toJSONString(wmap);
		String con = WeixinKit.sendJsonPost(url, json);
		System.out.println(con);
	}

	@Override
	public void moveUserToGroup(String openid, int gid) {
		String url =  WeixinKit.replaceAccToken(WeixinFianlValue.MOVE_USER_GROUP);
		Map<String,String> wmap = new HashMap<String,String>();
		wmap.put("openid", openid);
		wmap.put("to_groupid", ""+gid);
		String json = JSON.toJSONString(wmap);
		String con = WeixinKit.sendJsonPost(url, json);
		System.out.println(con);
	}

	
	@Override
	public void moveUsersToGroup(List<String> openids, int gid) {
		String url =  WeixinKit.replaceAccToken(WeixinFianlValue.MOVE_USERS_GROUP);
		Map<String,Object> wmap = new HashMap<String,Object>();
		wmap.put("openid_list", openids);
		wmap.put("to_groupid", "102");
		String json = JSON.toJSONString(wmap);
		String con = WeixinKit.sendJsonPost(url, json);
		System.out.println(con);
	}

	//删除分组
	@Override
	public void deleteGroup(int gid) {
		String url =  WeixinKit.replaceAccToken(WeixinFianlValue.DELETE_GROUP);
		Map<String,WGroup> wmap = new HashMap<String,WGroup>();
		WGroup wg = new WGroup();
		wg.setId(gid);
		wmap.put("group", wg);
		String json = JSON.toJSONString(wmap);
		String con = WeixinKit.sendJsonPost(url, json);
		System.out.println(con);
	}

}
